Module# 04: Arrays                                                                            Lecture#15: Vector for Arrays

 

// Example 15.1: Creating vector

 

// This program illustrates how a vector can be created.

 

import java.util.*;

 

class VectorCreateDemo1 {

    public static void main(String[] arg)  { 

        Vector v = new Vector();  // Create a vector of default size 10

        v.add(1);

        v.add(2);

        v.add("Debasis");

        v.add(3.4); 

        v.add("Samanta");

        System.out.println("Vector is "+ v);

    }

}

 

// Example 15.2: Creating a vector to store various types of elements

 

import java.util.*;

 

public class VectorCreateDemo2 {

   public static void main(String args[]) {

      // initial size is 3, increment is 2

      Vector v = new Vector(3, 2);

      System.out.println("Initial size: " + v.size());

      System.out.println("Initial capacity: " + v.capacity());

     

      v.addElement(new Integer(1));

      v.addElement(new Integer(2));

      v.addElement(new Integer(3));

      v.addElement(new Integer(4));

      System.out.println("Capacity after four additions: " + v.capacity());

 

      v.addElement(new Double(5.45));

      System.out.println("Current capacity: " + v.capacity());

 

      v.addElement(new Double(6.08));

      v.addElement(new Integer(7));

      System.out.println("Current capacity: " + v.capacity());

     

      v.addElement(new Float(9.4));

      v.addElement(new Integer(10));

      System.out.println("Current capacity: " + v.capacity());

     

      v.addElement(new Integer(11));

      v.addElement(new Integer(12));

      System.out.println("First element: " + (Integer)v.firstElement());

      System.out.println("Last element: " + (Integer)v.lastElement());

     

      if(v.contains(new Integer(3)))

         System.out.println("Vector contains 3.");

        

      // Enumerate the elements in the vector.

      Enumeration vEnum = v.elements();

      System.out.println("\nElements in vector:");

      while(vEnum.hasMoreElements())

         System.out.print(vEnum.nextElement() + " ");

      System.out.println();

   }

}

 

// Example 15.3: Insertion of elements into a vector

 

/* The following program shows how to insert the specified object as a component in this vector at the specified index. */

 

import java.util.*;

 

class VectorInsertionDemo1 {

    public static void main(String[] arg) {

        Vector vec = new Vector(7);

        // use add() method to add elements in the vector

        vec.add(1);

        vec.add(2);

        vec.add(3);

        vec.add(4);

        vec.add(5);

        vec.add(6);

        vec.add(7);

        // insert 10 at the index 7

        vec.insertElementAt(10, 7);

        // checking vector

        System.out.println(" Vector: " + vec);

    }

}

 

// Example 15.4: Inserting an element into a vector at specific position

 

// This program illustrates how an element can be inserted at a specified position in  vector.

 

import java.util.*;

 

class VectorInsertionDemo2 {

    public static void main(String[] arg) {

        Vector v = new Vector(5); // Create a default vector of size 5

        v.add(1, 1);

        v.add(2, 2);

        v.add(0, "Debasis");

        v.add(3, "Samanta");

        v.add(4, 3);

        v.add(5, 6.9);    // Vector will grow automatically

        // insert 10 at the index 7

        v.insertElementAt(7, 10);

        System.out.println("Vector is "+ v);

    }

}

 

// Example 15.5: Appending a set element into a vector

 

/* To append all of the elements in the specified Collection at the end of this Vector. */

 

import java.util.*;

class VectorInsertionDemo3 {

    public static void main(String[] arg)  {

        ArrayList arr = new ArrayList();

        arr.add(3);

        arr.add(“Oracle");

        arr.add(“Java");

        arr.add(4);

 

        Vector v = new Vector(); // Creating a default vector 

        v.addAll(arr); // copying all element of array list into vector

        System.out.println("vector v:"+ v); // checking vector v 

    }

}

 

// Example 15.6: Deletion of elements

 

// To remove all of the elements from a vector.

 

import java.util.*;

 

class VectorDeletionDemo1 {

    public static void main(String[] arg)  {

        Vector v = new Vector(); // The initial size of the vector is 10

        v.add(0, 1);

        v.add(1, 2);

        v.add(2, “IIT");

        v.add(3, “Kharagpur");

        v.add(4, 3);

        System.out.println("Vector is: "+ v);

        v.clear(); // Clearing the vector

        System.out.println(“After clearing: "+ v); // checking vector

    }

}

 

// Example 15.7: Deletion the first and last element from a vector

 

/* To remove an element at a specified location. It also illustrates the removal of of a specific element. */ 

 

import java.util.*;

 

class VectorDeletionDemo2 {

    public static void main(String[] arg) {

        Vector v = new Vector(); // Create a vector of (default) capacity 10

        v.add(1);

        v.add(2);

        v.add(“India");

        v.add(“Japan");

        v.add(4);

        v.removeElementAt(0); // Removing the element at 0, if it occurs

        System.out.println(“After removal: "+ v); // Checking vector

        v.removeElement(“Japan”);

        System.out.println(“After removal: "+ v); // Checking vector

    }

}

 

// Example 15.8: Deletion of common element from a vector

 

/* The following program shows how to retain only the elements in this Vector that are contained in the specified Collection. */ 

 

import java.util.*;

class VectorDeletionDemo3 {

    public static void main(String[] arg)

    {

        Vector vec = new Vector(7);

        Vector vecRetain = new Vector(4);

 

        // use add() method to add elements in the vector

        vec.add(1);

        vec.add(2);

        vec.add(3);

        vec.add(4);

        vec.add(5);

        vec.add(6);

        vec.add(7);

 

        // This elements will be retained

        vecRetain.add(5);

        vecRetain.add(3);

        vecRetain.add(2);

 

        System.out.println("Calling retainAll()");

        vec.retainAll(vecRetain);

 

        // Let us print all the elements available in vector

        System.out.println("Numbers after removal :- ");

 

        Iterator itr = vec.iterator();

 

        while (itr.hasNext()) {

            System.out.println(itr.next());

        }

    }

}

 

// Example 15.9: Searching an element

 

/* The following program shows to check if a specific element is present in a vector or not. */

 

import java.util.*;

class VectorSearchDemo1 {

    public static void main(String[] arg) {

 

        // create default vector

        Vector v = new Vector();

 

        v.add(1);

        v.add(2);

        v.add(“C++");

        v.add(“Python");

        v.add(3);

 

        // check whether vector contains "Java"

        if (v.contains("Java"))

            System.out.println("The element exists");

        else

            System.out.println("The element does notexist");

    }

}

 

// Example 15.10: Searching a specific item at a specific position

 

/* The following program shows how to return the index of the last occurrence of the specified element in this vector, or -1 if this vector does not contain the element. */

 

import java.util.*;

 

class VectorSearchDemo2 {

    public static void main(String[] arg) {

 

        // create default vector of capacity 10

        Vector v = new Vector();

 

        v.add(1);

        v.add(2);

        v.add("Oracle");

        v.add(2); 

        v.add("Java");

        v.add(4);

 

        // Checking last occurrence of 2

        System.out.println("last occurrence of 2 is: " + v.lastIndexOf(2));

    }

}

 

// Example 15.11: Replacement after Searching

 

/* The following program shows how to set the component at the specified index of this vector to be the specified object. */

 

 import java.util.*;

 

class VectorUpdateDemo {

    public static void main(String[] arg)

    {

        // Create default vector of capacity 10

        Vector v = new Vector();

 

        v.add(1);

        v.add(2);

        v.add("Mother");

        v.add("Merry");

        v.add(4);

 

        // Set 4 at the place of 2

        v.setElementAt(4, 1);

 

        System.out.println("vector: " + v);

    }

}

 

// Example 15.12: Cloning a vector

 

// This program duplicates a vector.

 

import java.util.*;

 

class VectorCloningDemo {

    public static void main(String[] arg) {

        Vector v = new Vector();

        Vector v_clone = new Vector();

        v.add(0, 1);

        v.add(1, 2);

        v.add(2, "Oracle");

        v.add(3, "Java");

        v.add(4, 3);

        v_clone = (Vector)v.clone();

        System.out.println("Clone of v: "+ v_clone);

    }

}

 

// Example 15.13: Copying a vector into a collection

 

/* The following program shows how to copy the components of this vector into the specified array. */

 

import java.util.*;

 

class VectorCopyDemo {

    public static void main(String[] arg) {

        Vector vec = new Vector(7);

        // Use add() method to add elements in the vector

        vec.add(1);

        vec.add(2);

        vec.add(3);

        vec.add(4);

        vec.add(5);

        vec.add(6);

        vec.add(7);

 

        Integer[] arr = new Integer[7];

 

        // Copy a vector into array arr

        vec.copyInto(arr);

 

        System.out.println("elements in array arr: ");

        for (Integer num : arr) {

            System.out.println(num);

        }

    }

}

 

 

// Example 15.14: Equality checking of two collections

 

/* The following program shows how to compare the specified Object with this Vector for equality.. */

 

import java.util.*;

 

class VectorCheckDemo {

    public static void main(String[] arg) {

        // Create default vector of capacity 10

        Vector v = new Vector();

 

        v.add(1);

        v.add(2);

        v.add("Oracle");

        v.add("Java");

        v.add(4);

 

        // Second vector

        Vector v_2nd = new Vector();

        v_2nd.add(1);

        v_2nd.add(2);

        v_2nd.add("Java");

        v_2nd.add("Oracle");

        v_2nd.add(4);

 

        if (v.equals(v_2nd))

            System.out.println("both vectors are equal");

    }

}

 

 

 

 

// Example 15.15: String representation of a vector

 

/* The following program shows how the toString() method is used to return a string representation of this Vector, containing the String representation of each element. */

 

import java.util.*;

 

class VectorStringDemo {

    public static void main(String[] arg)  {

        // create default vector of capacity 10

        Vector v = new Vector();

 

        v.add(1);

        v.add(2);

        v.add(”Debasis");

        v.add(”Samanta");

        v.add(4);

 

        // string equivalent of vector

        System.out.println(" String equivalent of vector: " + v.toString());

    }

}

 

// Example 15.16: Hashcode representation of a vector

 

/* The following program shows how to return the hash code value for this Vector. */

 

import java.util.*;

 

class VectorHashcodeDemo {

    public static void main(String[] arg)

    {

        Vector vec = new Vector(7);

 

        // use add() method to add elements in the vector

        vec.add(1);

        vec.add(2);

        vec.add(3);

        vec.add(4);

        vec.add(5);

        vec.add(6);

        vec.add(7);

 

        // checking hash code

        System.out.println("Hash code: " + vec.hashCode());

    }

}